//
//  HOBaseReportViewController.m
//  微度报修
//
//  Created by foguo-mac-1 on 6/15/16.
//  Copyright © 2016 WeiDu. All rights reserved.
//

#import "HOBaseReportViewController.h"
#import "HOEditView.h"
#import "HOUtils.h"
#import "HOConstants.h"
#import "HODepartmentViewController.h"
#import "HOFaultCategoryViewController.h"
#import "HOStaticDataMgr.h"
#import "HOVoiceDownloader.h"
#import "HOBigImageViewController.h"
#import "HODefine.h"
#import "HODefaultImage.h"
#import "HOUIMgr.h"
#import "HOGlobalData.h"
#import "HOQuickReportViewController.h"
#import "HOHybridTypeViewController.h"
#import "HOUIMgr.h"
#import "VoiceConverter.h"

@interface HOBaseReportViewController ()<HODepartmentViewControllerDelegate, HOFaultCategoryViewControllerDelegate, UIAlertViewDelegate, HOHybridTypeViewControllerDelegate>
{
	BOOL isReturnFromDeptmentList;
}

@end

@implementation HOBaseReportViewController
- (void)viewDidLoad {
    [super viewDidLoad];
	// Do any additional setup after loading the view.
	[self initEditView];
	[self registerTapGesture];
	[self registerKeyboard];
//	[self initReportAreaFromLocal];
//	[self initReportDepartment];
}

- (void)submit
{
	UIButton *btn = (UIButton *)[baseTableview.tableFooterView viewWithTag:111];
	btn.enabled = YES;
}

- (void)viewWillAppear:(BOOL)animated
{
	[super viewWillAppear:animated];
	if(!isReturnFromDeptmentList)
		[self initReportGategoryFromLocal];
	isReturnFromDeptmentList = NO;
}

- (void)viewWillDisappear:(BOOL)animated
{
	[super viewWillDisappear:animated];
}

- (void)initEditView
{
	self.reportDescEditView = [[[HOEditView alloc] initWithFrame:CGRectMake(0,
																	  0,
																	  self.view.frame.size.width,
																	  134)] autorelease];
	self.reportDescEditView.limitedCount = 120;
	self.reportDescEditView.delegate = self;
	self.reportDescEditView.layer.cornerRadius = 0;
	self.reportDescEditView.layer.borderWidth = 0;
	[self.reportDescEditView.layer setMasksToBounds:YES];
	[self.reportDescEditView setPlaceHolder:NSLocalizedString(@"故障情况描述", @"故障情况描述")];
	[self.reportDescEditView getTextView].returnKeyType = UIReturnKeyDone;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)hideKeyboard
{
	[super hideKeyboard];
	
	if(self.reportDescEditView)
	{
		[[self.reportDescEditView getTextView] resignFirstResponder];
	}
}

- (BOOL)isSelectFaultType:(UIButton *)btn
{
//	NSString *faultDetail = [self getTrimingCharaFromTextView:[self.reportDescEditView getTextView]];

	if(self.selectedDepartmentEntity == nil)
	{
		[self popupHintView:NSLocalizedString(@"请选择负责部门！", @"请选择负责部门！")];
		return NO;
	}
	if(self.selectedFaultGategoryEntity == nil)
	{
		[self popupHintView:NSLocalizedString(@"请选择故障分类！", @"请选择故障分类！")];
		return NO;
	}
	return YES;
}

- (void)viewDidDisappear:(BOOL)animated
{
	[super viewDidDisappear:animated];
	[self.recordView stopAudioPlay];
}

- (void)sendNow:(HOEditView *)endView
{
//	[self didClickFooterButton:nil];
	[self hideKeyboard];
}

- (CGFloat)getDescEditViewHeight
{
	return self.reportDescEditView.frame.size.height;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForTakePhotoRowAtIndexPath:(NSIndexPath *)indexPath
{
	static NSString *HOTakePhotoTableViewCellIdentifier = @"HOTakePhotoTableViewCellIdentifier";
	HOTableViewCell *cell = (HOTableViewCell *)[tableView dequeueReusableCellWithIdentifier:HOTakePhotoTableViewCellIdentifier];
	if(cell == nil)
	{
		cell = [[[HOTableViewCell alloc]
				 initWithStyle:UITableViewCellStyleDefault
				 reuseIdentifier: HOTakePhotoTableViewCellIdentifier] autorelease];
	}
//	CGFloat cellHeight = [self tableView:tableView heightForRowAtIndexPath:indexPath];
	cell.textLabel.font = [UIFont systemFontOfSize:15];
	cell.textLabel.textColor = TEXT_COLOR_181818;
	if(indexPath.row == 0)
	{
		cell.accessoryType = UITableViewCellAccessoryNone;
		cell.selectionStyle = UITableViewCellSelectionStyleNone;
		HOEditView *view = (HOEditView *)[cell viewWithTag:115];
		if(view == nil)
		{
			[cell.contentView addSubview:self.reportDescEditView];
			self.reportDescEditView.tag = 115;
		}
		
		HOUploadImageView *uploadImgView = (HOUploadImageView *)[cell viewWithTag:114];
		if(uploadImgView == nil)
		{
			[cell.contentView addSubview:self.uploadImgView];
			uploadImgView.tag = 114;
		}
	}
	else if(indexPath.row == 1)
	{
		cell.selectionStyle = UITableViewCellSelectionStyleNone;
		HORecordView *view = (HORecordView *)[cell viewWithTag:116];
		if(view == nil)
		{
			[cell.contentView addSubview:self.recordView];
			self.recordView.tag = 116;
		}
		
//		UIButton *checkBtn = (UIButton *)[cell viewWithTag:216];
//		if(checkBtn == nil)
//		{
//			checkBtn = [[[UIButton alloc]
//						 initWithFrame:CGRectMake(HO_HORIZONAL_PADDING,
//												  128 + 22 + (50 - 24) / 2,
//												  24,
//												  24)] autorelease];
//			[checkBtn setBackgroundImage:[UIImage imageNamed:@"IC_putongbaoxiu_kuang"] forState:UIControlStateNormal];
//			[checkBtn addTarget:self action:@selector(didClickCheckBtn:) forControlEvents:UIControlEventTouchUpInside];
//			[cell.contentView addSubview:checkBtn];
//		}
//		if(self.isNeedRerepair)
//		{
//			[checkBtn setBackgroundImage:[UIImage imageNamed:@"IC_putongbaoxiu_gou"] forState:UIControlStateNormal];
//		}
//		else
//		{
//			[checkBtn setBackgroundImage:[UIImage imageNamed:@"IC_putongbaoxiu_kuang"] forState:UIControlStateNormal];
//		}
//
//		checkBtn.enabled = [self isEnableCheckButton];
		
//		UILabel *lbl = (UILabel *)[cell viewWithTag:117];
//		if(!lbl)
//		{
//			lbl = [[UILabel alloc] init];
//			lbl.frame = CGRectMake(checkBtn.frame.size.width + checkBtn.frame.origin.x + HO_HORIZONAL_PADDING,
//										128 + 22 + (50 - 22) / 2,
//										100,
//										22);
//			lbl.tag = 117;
//			lbl.textColor = TEXT_COLOR_181818;
//			lbl.font = [UIFont systemFontOfSize:15];
//			lbl.text = NSLocalizedString(@"是否返修", @"是否返修");
//			[cell.contentView addSubview:lbl];
//			[lbl release];
//		}
	}
	return cell;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForOrderDetailRowAtIndexPath:(NSIndexPath *)indexPath
{
	static NSString *HOTakePhotoTableViewCellIdentifier = @"HOTakePhotoTableViewCellIdentifier";
	HOTableViewCell *cell = (HOTableViewCell *)[tableView dequeueReusableCellWithIdentifier:HOTakePhotoTableViewCellIdentifier];
	if(cell == nil)
	{
		cell = [[[HOTableViewCell alloc]
				 initWithStyle:UITableViewCellStyleDefault
				 reuseIdentifier: HOTakePhotoTableViewCellIdentifier] autorelease];
	}
	cell.textLabel.font = [UIFont systemFontOfSize:15];
	cell.textLabel.textColor = TEXT_COLOR_181818;
	if(indexPath.row == 0)
	{
		cell.accessoryType = UITableViewCellAccessoryNone;
		cell.selectionStyle = UITableViewCellSelectionStyleNone;
		
		HOUploadImageView *uploadImgView = (HOUploadImageView *)[cell viewWithTag:114];
		if(uploadImgView == nil)
		{
			[cell.contentView addSubview:self.uploadImgView];
			self.uploadImgView.frame = CGRectMake(0,
												  HO_HORIZONAL_PADDING,
												  tableView.frame.size.width,
												  self.singleImgWidth);
			uploadImgView.tag = 114;
		}
		
	}
	else if(indexPath.row == 1)
	{
		cell.selectionStyle = UITableViewCellSelectionStyleNone;
		HORecordView *view = (HORecordView *)[cell viewWithTag:116];
		if(view == nil)
		{
			[cell.contentView addSubview:self.recordView];
			self.recordView.tag = 116;
		}
		
//		UIButton *checkBtn = (UIButton *)[cell viewWithTag:216];
//		if(checkBtn == nil)
//		{
//			checkBtn = [[[UIButton alloc]
//						 initWithFrame:CGRectMake(tableView.frame.size.width - 135 + 30,
//												  (50 - 24) / 2,
//												  24,
//												  24)] autorelease];
//			[checkBtn setBackgroundImage:[UIImage imageNamed:@"IC_putongbaoxiu_kuang"] forState:UIControlStateNormal];
//			[checkBtn addTarget:self action:@selector(didClickCheckBtn:) forControlEvents:UIControlEventTouchUpInside];
//			[cell.contentView addSubview:checkBtn];
//			checkBtn.tag = 216;
//		}
//		if(self.isNeedRerepair)
//		{
//			[checkBtn setBackgroundImage:[UIImage imageNamed:@"IC_putongbaoxiu_gou"] forState:UIControlStateNormal];
//		}
//		else
//		{
//			[checkBtn setBackgroundImage:[UIImage imageNamed:@"IC_putongbaoxiu_kuang"] forState:UIControlStateNormal];
//		}
//		checkBtn.enabled = [self isEnableCheckButton];
//		
//		
//		UILabel *lbl = (UILabel *)[cell viewWithTag:117];
//		if(!lbl)
//		{
//			lbl = [[UILabel alloc] init];
//			lbl.frame = CGRectMake(tableView.frame.size.width - 135 + 60,
//								   (50 - 22) / 2,
//								   100,
//								   22);
//			lbl.tag = 117;
//			lbl.textColor = TEXT_COLOR_181818;
//			lbl.font = [UIFont systemFontOfSize:15];
//			lbl.text = NSLocalizedString(@"是否返修", @"是否返修");
//			[cell.contentView addSubview:lbl];
//			[lbl release];
//		}
	}
	return cell;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForPatrolRowAtIndexPath:(NSIndexPath *)indexPath
{
	static NSString *HOTakePhotoTableViewCellIdentifier = @"HOTakePhotoTableViewCellIdentifier";
	HOTableViewCell *cell = (HOTableViewCell *)[tableView dequeueReusableCellWithIdentifier:HOTakePhotoTableViewCellIdentifier];
	if(cell == nil)
	{
		cell = [[[HOTableViewCell alloc]
				 initWithStyle:UITableViewCellStyleDefault
				 reuseIdentifier: HOTakePhotoTableViewCellIdentifier] autorelease];
	}
//	CGFloat cellHeight = [self tableView:tableView heightForRowAtIndexPath:indexPath];
	cell.textLabel.font = [UIFont systemFontOfSize:15];
	cell.textLabel.textColor = TEXT_COLOR_181818;
	if(indexPath.row == 0)
	{
		cell.accessoryType = UITableViewCellAccessoryNone;
		cell.selectionStyle = UITableViewCellSelectionStyleNone;
		HOEditView *view = (HOEditView *)[cell viewWithTag:115];
		if(view == nil)
		{
			[cell.contentView addSubview:self.reportDescEditView];
			self.reportDescEditView.tag = 115;
		}
		
		HOUploadImageView *uploadImgView = (HOUploadImageView *)[cell viewWithTag:114];
		if(uploadImgView == nil)
		{
			[cell.contentView addSubview:self.uploadImgView];
			uploadImgView.tag = 114;
		}
	}
	return cell;
}

- (void)didClickCheckBtn:(UISwitch *)switcher
{
	self.isNeedRerepair = ! self.isNeedRerepair;
	[baseTableview reloadData];
}

- (void)didSelectForDepartmentRowAtIndexPath:(NSIndexPath *)indexPath
{
	if(indexPath.row == 0)
	{
		HODepartmentViewController *viewCtrl = [[HODepartmentViewController alloc] init];
		viewCtrl.delegate = self;
		[self.navigationController pushViewController:viewCtrl animated:YES];
		[viewCtrl release];
	}
	else if(indexPath.row == 1)
	{
		if(self.selectedDepartmentEntity == nil)
		{
			[self popupHintView:NSLocalizedString(@"请先选择负责部门！", @"请先选择负责部门！")];
			return;
		}
		HOHybridTypeViewController *viewCtrl = [[HOHybridTypeViewController alloc] init];
		viewCtrl.delegate = self;
		viewCtrl.currentEntity = self.selectedFaultGategoryEntity;
		viewCtrl.subEntity = self.selectedSubFaultGategoryEntity;
		viewCtrl.currentDepartmentEntity = self.selectedDepartmentEntity;
		[self.navigationController pushViewController:viewCtrl animated:YES];
		[viewCtrl release];
	}
	
}


- (void)didSelectDepartment:(HODepartmentEntity *)entity
{
	
	if(entity.departmentId == self.selectedDepartmentEntity.departmentId)
		return;
	if(self.selectedDepartmentEntity == nil
	   || self.selectedDepartmentEntity.departmentId != entity.departmentId)
	{
		self.hopeHandler = nil;
	}
	self.selectedDepartmentEntity = entity;
	[self saveReportDepartment];
	[[HOGlobalData sharedInstance] clearReportType];
	self.selectedFaultGategoryEntity = nil;
	self.selectedSubFaultGategoryEntity = nil;
	isReturnFromDeptmentList = YES;
	
	[baseTableview reloadData];
}
- (void)didSelectFault:(HOFaultCategoryEntity *)entity withSubEntity:(HOFaultCategoryEntity *)subEntity
{
	self.selectedFaultGategoryEntity = entity;
	self.selectedSubFaultGategoryEntity = subEntity;
    [self saveReportTypeIntoLocal];
	[baseTableview reloadData];
}


#pragma mark
- (void)didSelectAreaEntity:(HOAreaEntity *)entity secondEntity:(HOAreaEntity *)secondEntity withThirdArea:(HOAreaEntity *)thirdEntity
{
	self.selectedFirstAreaEntity = entity;
	self.selectedSecondAreaEntity = secondEntity;
	self.selectedThirdAreaEntity = thirdEntity;
	[self saveReportAreaIntoLocal];
	[baseTableview reloadData];
}

- (void)didSelectFault:(HOFaultCategoryEntity *)entity
		 withSubEntity:(HOFaultCategoryEntity *)subEntity
		withDepartment:(HODepartmentEntity *)department
{
	[self didSelectDepartment:department];
	[self didSelectFault:entity withSubEntity:subEntity];
}

- (NSString *)getSelectArea
{
	NSMutableString *areaIds = [[[NSMutableString alloc] initWithString:@""] autorelease];
	if(self.selectedFirstAreaEntity == nil || self.selectedSecondAreaEntity == nil)
		return areaIds;
	[areaIds appendFormat:@"%d", self.selectedFirstAreaEntity.areaId];
	
	if(self.selectedSecondAreaEntity == nil)
		return areaIds;
	[areaIds appendFormat:@"-%d", self.selectedSecondAreaEntity.areaId];
	
	if(self.selectedThirdAreaEntity == nil)
		return areaIds;
	[areaIds appendFormat:@"-%d", self.selectedThirdAreaEntity.areaId];
	
	return areaIds;
}

- (NSString *)getSelectAreaName
{
	NSMutableString *areaNames = [[[NSMutableString alloc] initWithString:@""] autorelease];
	if(self.selectedFirstAreaEntity == nil || self.selectedSecondAreaEntity == nil)
		return areaNames;
	[areaNames appendFormat:@"%@", self.selectedFirstAreaEntity.areaName];
	
	if(self.selectedSecondAreaEntity == nil)
		return areaNames;
	[areaNames appendFormat:@"-%@", self.selectedSecondAreaEntity.areaName];
	
	if(self.selectedThirdAreaEntity == nil)
		return areaNames;
	[areaNames appendFormat:@"-%@", self.selectedThirdAreaEntity.areaName];
	
	
	return areaNames;
}

- (NSString *)getSelectTypeIds
{
	NSMutableString *typeIds = [[[NSMutableString alloc] initWithString:@""] autorelease];
	if(self.selectedFaultGategoryEntity == nil || self.selectedSubFaultGategoryEntity == nil)
		return typeIds;
	[typeIds appendFormat:@"%d", self.selectedFaultGategoryEntity.faultId];
	
	if(self.selectedSubFaultGategoryEntity == nil)
		return typeIds;
	[typeIds appendFormat:@"-%d", self.selectedSubFaultGategoryEntity.faultId];
	
	return typeIds;
}

- (NSString *)getSelectTypeNames
{
	if(self.selectedDepartmentEntity == nil || self.selectedDepartmentEntity.departmentName == nil)
		return @"";
	NSString *deptStr = [NSString stringWithFormat:@"[%@]", self.selectedDepartmentEntity.departmentName];
	NSMutableString *typeNames = [[[NSMutableString alloc] initWithString:deptStr] autorelease];
	if(self.selectedFaultGategoryEntity == nil || self.selectedSubFaultGategoryEntity == nil)
		return typeNames;
	[typeNames appendFormat:@"-%@", self.selectedFaultGategoryEntity.faultDetail];
	
	if(self.selectedSubFaultGategoryEntity == nil)
		return typeNames;
	[typeNames appendFormat:@"-%@", self.selectedSubFaultGategoryEntity.faultDetail];

	return typeNames;
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
	[super alertView:alertView clickedButtonAtIndex:buttonIndex];
	if(alertView.tag == [kHO_DIALOG_ReportSucc integerValue] &&  buttonIndex == 1)
	{
		[HOStaticDataMgr sharedInstance].needGotoMyReport = YES;
		[self sendCommand:kHO_CMD_gotoMy data:nil];
		[self.navigationController popViewControllerAnimated:NO];
	}
}

- (void)didSubmitReport
{
	if([HOUIMgr sharedInstance].currentViewController != self)
		return;
	HOAlertView *amAlertView = [[HOAlertView alloc] initWithTitle:NSLocalizedString(@"提交报修成功！请到“我的报修”里查看相关信息。",  @"提交报修成功！请到“我的报修”里查看相关信息。")
							   message: nil
							  delegate: self
					 cancelButtonTitle:NSLocalizedString(@"关闭", @"关闭")
					 otherButtonTitles:NSLocalizedString(@"确定", @"确定"), nil];
	amAlertView.tag = [kHO_DIALOG_ReportSucc integerValue];
	[amAlertView show];
	[amAlertView release];
}

- (void)didReceiveCommand:(NSString *)action data:(id)data
{
//	NSLog(@"data:%@", data);
//	NSLog(@"self.voiceFileURL:%@", self.voiceFileURL);
	if([action isEqualToString: kHO_CMD_voiceDidDownload])
	{
		if([data isEqualToString:self.voiceFileURL])
		{
			self.voiceFileName = [HOVoiceDownloader getFileNameFromURL:self.voiceFileURL];
			[self.recordView setAudioWithTotalTime:self.totalTime withFileName:self.voiceFileName];
			[baseTableview reloadData];
		}
	}
	if([action isEqualToString:kHO_CMD_ClearRecord])
	{
//		baseTableview.scrollEnabled = NO;
		[self cleanupAudio];
	}
	else if([action isEqualToString:kHO_CMD_Report_Clear_Report])
	{
		[self cleanup];
	}
}

- (void)saveReportAreaIntoLocal
{
	NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
	
	if(self.selectedFirstAreaEntity && self.selectedFirstAreaEntity.areaId > 0 && self.selectedFirstAreaEntity.areaName != nil)
	{
		[userDefault setObject:self.selectedFirstAreaEntity.areaName forKey:[self getKeyForSaveLocal:kHO_FIELD_FaultAreaName]];
		[userDefault setObject:[NSNumber numberWithInt:self.selectedFirstAreaEntity.areaId] forKey:[self getKeyForSaveLocal:kHO_FIELD_FaultAreaId]];
		[userDefault setObject:[NSNumber numberWithInteger:self.selectedFirstAreaEntity.index] forKey:[self getKeyForSaveLocal:kHO_FIELD_FaultAreaIndex]];
	}
	
	if(self.selectedSecondAreaEntity && self.selectedSecondAreaEntity.areaId > 0 && self.selectedSecondAreaEntity.areaName != nil)
	{
		[userDefault setObject:self.selectedSecondAreaEntity.areaName forKey:[self getKeyForSaveLocal:kHO_FIELD_FaultSubAreaName]];
		[userDefault setObject:[NSNumber numberWithInt:self.selectedSecondAreaEntity.areaId] forKey:[self getKeyForSaveLocal:kHO_FIELD_FaultSubAreaId]];
		[userDefault setObject:[NSNumber numberWithInt:self.selectedSecondAreaEntity.areaId] forKey:[self getKeyForSaveLocal:kHO_FIELD_FaultSubAreaId]];
		[userDefault setObject:[NSNumber numberWithInteger:self.selectedSecondAreaEntity.index] forKey:[self getKeyForSaveLocal:kHO_FIELD_FaultSubAreaIndex]];
	}
	else
	{
		[userDefault removeObjectForKey:[self getKeyForSaveLocal:kHO_FIELD_FaultSubAreaId]];
		[userDefault removeObjectForKey:[self getKeyForSaveLocal:kHO_FIELD_FaultSubAreaName]];
	}
	
	if(self.selectedThirdAreaEntity && self.selectedThirdAreaEntity.areaId > 0 && self.selectedThirdAreaEntity.areaName != nil)
	{
		[userDefault setObject:self.selectedThirdAreaEntity.areaName forKey:[self getKeyForSaveLocal:kHO_FIELD_FaultThirdAreaName]];
		[userDefault setObject:[NSNumber numberWithInt:self.selectedThirdAreaEntity.areaId] forKey:[self getKeyForSaveLocal:kHO_FIELD_FaultThirdAreaId]];
		[userDefault setObject:[NSNumber numberWithInteger:self.selectedThirdAreaEntity.index] forKey:[self getKeyForSaveLocal:kHO_FIELD_FaultThirdAreaIndex]];
	}
	else
	{
		[userDefault removeObjectForKey:[self getKeyForSaveLocal:kHO_FIELD_FaultThirdAreaId]];
		[userDefault removeObjectForKey:[self getKeyForSaveLocal:kHO_FIELD_FaultThirdAreaName]];
	}
	
	[userDefault synchronize];
}

- (void)initReportDepartment
{
	NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
	int departmentId = [[userDefault objectForKey:[self getKeyForSaveLocal:kHO_FIELD_DepartmentId]] intValue];
	NSString *departmentName = [userDefault objectForKey:[self getKeyForSaveLocal:kHO_FIELD_DepartmentName]];
	if(departmentId == 0 || departmentName == nil)
	{
		self.selectedDepartmentEntity = nil;
		return;
	}
	
	self.selectedDepartmentEntity = [[HOStaticDataMgr sharedInstance] getDepartmentEntityWithId:departmentId];
	self.selectedDepartmentEntity.departmentId = departmentId;
	self.selectedDepartmentEntity.departmentName = departmentName;
}

- (void)saveReportDepartment
{
	if(self.selectedDepartmentEntity == nil)
		return;
	NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
	[userDefault setObject:self.selectedDepartmentEntity.departmentName forKey:[self getKeyForSaveLocal:kHO_FIELD_DepartmentName]];
	[userDefault setObject:[NSNumber numberWithInt:self.selectedDepartmentEntity.departmentId] forKey:[self getKeyForSaveLocal:kHO_FIELD_DepartmentId]];
	[userDefault synchronize];
}

- (NSString *)getKeyForSaveLocal:(NSString *)field
{
	NSString *key = [NSString stringWithFormat:@"%@_%@", field, [[HOStaticDataMgr sharedInstance] getCompanyId]];
	return key;
}

- (void)initReportAreaFromLocal
{
	NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
	
	int areaId 				= [[userDefault objectForKey:[self getKeyForSaveLocal:kHO_FIELD_FaultAreaId]] intValue];
	NSString *areaName 		= [userDefault objectForKey: [self getKeyForSaveLocal:kHO_FIELD_FaultAreaName]];
	NSInteger index 		= [[userDefault objectForKey:[self getKeyForSaveLocal:kHO_FIELD_FaultAreaIndex]] integerValue];
	
	int subAreaId 			= [[userDefault objectForKey:[self getKeyForSaveLocal:kHO_FIELD_FaultSubAreaId]] intValue];
	NSString *subAreaName 	= [userDefault objectForKey: [self getKeyForSaveLocal:kHO_FIELD_FaultSubAreaName]];
	NSInteger subIndex 		= [[userDefault objectForKey:[self getKeyForSaveLocal:kHO_FIELD_FaultSubAreaIndex]] integerValue];
	
	int thirdAreaId 		= [[userDefault objectForKey:[self getKeyForSaveLocal:kHO_FIELD_FaultThirdAreaId]] intValue];
	NSString *thirdAreaName = [userDefault objectForKey: [self getKeyForSaveLocal:kHO_FIELD_FaultThirdAreaName]];
	NSInteger thirdIndex 	= [[userDefault objectForKey:[self getKeyForSaveLocal:kHO_FIELD_FaultThirdAreaIndex]] integerValue];
	
	if(areaId == 0 || !areaName)
	{
		self.selectedFirstAreaEntity = nil;
		self.selectedSecondAreaEntity = nil;
		self.selectedThirdAreaEntity = nil;
		return;
	}
	self.selectedFirstAreaEntity = [[HOStaticDataMgr sharedInstance] getAreaEntityWithId:areaId];
	self.selectedFirstAreaEntity.areaId = areaId;
	self.selectedFirstAreaEntity.areaName = areaName;
	self.selectedFirstAreaEntity.index = index;
	
	if(subAreaId == 0 || !subAreaName)
	{
		self.selectedFirstAreaEntity = nil;
		self.selectedSecondAreaEntity = nil;
		self.selectedThirdAreaEntity = nil;
		return;
	}
	self.selectedSecondAreaEntity = [[HOStaticDataMgr sharedInstance] getAreaEntityWithId:subAreaId];
	self.selectedSecondAreaEntity.areaId = subAreaId;
	self.selectedSecondAreaEntity.areaName = subAreaName;
	self.selectedSecondAreaEntity.index = subIndex;
	
	if(thirdAreaId == 0 || !thirdAreaName)
	{
		self.selectedThirdAreaEntity = nil;
		return;
	}
	
	self.selectedThirdAreaEntity = [[HOStaticDataMgr sharedInstance] getAreaEntityWithId:thirdAreaId];
	self.selectedThirdAreaEntity.areaId = thirdAreaId;
	self.selectedThirdAreaEntity.areaName = thirdAreaName;
	self.selectedThirdAreaEntity.index = thirdIndex;
	
	
}

- (void)saveReportTypeIntoLocal
{
    if(self.selectedFaultGategoryEntity == nil)
        return;
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    [userDefault setObject:self.selectedFaultGategoryEntity.faultDetail forKey:[self getKeyForSaveLocal:kHO_FIELD_FaultTypeName]];
    [userDefault setObject:[NSNumber numberWithInt:self.selectedFaultGategoryEntity.faultId] forKey:[self getKeyForSaveLocal:kHO_FIELD_FaultTypeId]];
	
	if(self.selectedSubFaultGategoryEntity)
	{
		
		[userDefault setObject:self.selectedSubFaultGategoryEntity.faultDetail forKey:[self getKeyForSaveLocal:kHO_FIELD_FaultSubTypeName]];
		[userDefault setObject:[NSNumber numberWithInt:self.selectedSubFaultGategoryEntity.faultId] forKey:[self getKeyForSaveLocal:kHO_FIELD_FaultSubTypeId]];
	}
    [userDefault synchronize];
}

- (void)initReportGategoryFromLocal
{
	NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    int typeId = [[userDefault objectForKey:[self getKeyForSaveLocal:kHO_FIELD_FaultTypeId]] intValue];
	if(typeId == 0 )
	{
		self.selectedFaultGategoryEntity = nil;
		self.selectedSubFaultGategoryEntity = nil;
		return;
	}
    
    NSString *typeName = [userDefault objectForKey:[self getKeyForSaveLocal:kHO_FIELD_FaultTypeName]];
	if(typeName == NULL)
	{
		self.selectedFaultGategoryEntity = nil;
		self.selectedSubFaultGategoryEntity = nil;
		return;
	}
	
	self.selectedFaultGategoryEntity = [[HOStaticDataMgr sharedInstance] getFaultCategoryEntityWithId:typeId];
	self.selectedFaultGategoryEntity.faultId = typeId;
	self.selectedFaultGategoryEntity.faultDetail = typeName;
	
	int subTypeId = [[userDefault objectForKey:[self getKeyForSaveLocal:kHO_FIELD_FaultSubTypeId]] intValue];
	if(subTypeId == 0)
	{
		self.selectedFaultGategoryEntity = nil;
		self.selectedSubFaultGategoryEntity = nil;
		return;
	}
	NSString *subTypeName = [userDefault objectForKey:[self getKeyForSaveLocal:kHO_FIELD_FaultSubTypeName]];
	if(subTypeName == NULL)
	{
		self.selectedFaultGategoryEntity = nil;
		self.selectedSubFaultGategoryEntity = nil;
		return;
	}
	
	self.selectedSubFaultGategoryEntity = [[HOStaticDataMgr sharedInstance] getFaultCategoryEntityWithId:subTypeId];
	self.selectedSubFaultGategoryEntity.faultId = subTypeId;
	self.selectedSubFaultGategoryEntity.faultDetail = subTypeName;
}

#pragma mark
- (void)cleanup
{
	self.selectedDepartmentEntity = nil;
	self.selectedSubDepartmentEntity = nil;
	self.selectedFaultGategoryEntity = nil;
	self.selectedSubFaultGategoryEntity = nil;
	
	[self.reportDescEditView getTextView].text = @"";
	[super cleanup];
}

- (void)dealloc
{
	[self cleanup];
	self.reportDescEditView = nil;
	[super dealloc];
}
@end
